<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单配送
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */


quanli_require('module/base/system/itemHandler.class.php');
quanli_require('module/base/system/sysDistribution.class.php');
quanli_require('module/base/system/sysDistributionTechnician.class.php');

class pa_orderAllocation {

    public function execute() {
        global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //订单配送
                    $this->orderAllocationList();
                    break;
                default:
                    $G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 订单配送
     */
    private function orderAllocationList() {
        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

       //保存配送结束时间最晚的时间
        $end_time = '';
        if($sendEndTime_array){
            $result_data = end($sendEndTime_array);
            $end_time = $result_data['itemName'];
        }

        $PS_LOGIN_SESSION = $G_PGLOB->sessionCache_get('PS_LOGIN_SESSION');


        $distribution_array = array();
        if( $PS_LOGIN_SESSION['city'] ){
            $key = 0;
            foreach($PS_LOGIN_SESSION['city'] as $city){
                //实例化配送网点设置类
                $sysDistribution = new sysDistribution( ) ;

                $arg_array = array(
                    'cityid' => $city,
                    'useFlg' => 1
                );

                $return_array = $sysDistribution->search( $arg_array ) ;

                if( $return_array ){
                    foreach($return_array as $distribution){
                        if( in_array( $distribution['distribution_id'],$PS_LOGIN_SESSION['distribution'] ) ){
                            $distribution_array[$key]['distribution_id'] = $distribution['distribution_id'];
                            $distribution_array[$key]['distribution_name'] = $distribution['distribution_name'];
                            $key ++;
                        }
                    }
                }

            }
        }

        $technician_array = array();
        if( $distribution_array ){
            $key = 0;
            foreach($distribution_array as $distribution){
                //实例化配送员类
                $sysDistributionTechnician = new sysDistributionTechnician();

                $arg_array = array(
                    'distribution_id' => $distribution['distribution_id'],
                    'useFlg' => 1
                );

                $return_array = $sysDistributionTechnician->search( $arg_array );

                if($return_array){
                    foreach($return_array as $technician){
                        $technician_array[$key]['distribution_technician_id'] = $technician['distribution_technician_id'];
                        $technician_array[$key]['distribution_technician_name'] = $technician['distribution_technician_name'];
                        $technician_array[$key]['mobile'] = $technician['mobile'];
                        $key ++;
                    }

                }

            }
        }

        $G_PGLOB->smarty->assign_by_ref('end_time',$end_time);
        $G_PGLOB->smarty->assign_by_ref('distribution_array',$distribution_array);
        $G_PGLOB->smarty->assign_by_ref('technician_array',$technician_array);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(20509));
        $G_PGLOB->smarty->assign('currentDate',$G_PGLOB->sys_date);//当前日期

        $table_id = 4;

        $flex_array = array(
            array('display' => '<input type="checkbox" id="allChecked" />','width' => 60,'align' => 'center'),
            array('display' => '订单编号','width' => 145, 'align' => 'center'),
            array('display' => '订单标识','width' => 100),
            array('display' => '退货','width' => 60),
            array('display' => '订单类型','width' => 100),
            array('display' => '订单状态','width' => 100),
            array('display' => '配送日期','width' => 100, 'align' => 'center'),
            array('display' => '配送开始时间','name'=> 'send_start_time','sortable'=> true,'width' => 80),
            array('display' => '配送结束时间','width' => 80),
            array('display' => '下单时间','width' => 140,'align' => 'center'),
            array('display' => '配送站','width' => 150),
            array('display' => '配送员','width' => 80,'name' => 'distribution_technician_id','sortable' => true),
            array('display' => '城市','width' => 80),
            array('display' => '区','width' => 80),
            array('display' => '收货地址','width' => 300,'name' => 'deliver_address','sortable' => true),
            array('display' => '收货人','width' => 100),
            array('display' => '收货人手机','width' => 100),
            array('display' => '固定电话','width' => 100),
            array('display' => '发票','width' => 80),
            array('display' => '蛋糕数量','width' => 80),
            array('display' => '蛋糕规格','width' => 120),
            array('display' => '线下付款方式','width' => 100),
            array('display' => '应收金额', 'width' => 80),
            array('display' => '配送备注', 'width' => 120),
        ); //flexigrid插件表格  end

        quanli_require('module/base/system/sysFlexigridLog.class.php');

        $sysFlexigridLog = new sysFlexigridLog();

        $arg_array = array(
            'table_id' => $table_id,
            'userID' => $G_PGLOB->currentUserID,
            'sortname' => 'last_cid',
            'sortorder' => 'ASC'
        );
        $flexigrid_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flexigrid_array){
            $flex_array = $flexigrid_array;
        }else{
            $arg_array = array(
                'table_id' => $table_id,
                'userID' => $G_PGLOB->currentUserID,
                'data' => $flex_array
            );
            $sysFlexigridLog->editFlexigrid($arg_array);
        }

        $G_PGLOB->smarty->assign_by_ref('table_id', $table_id);
        $G_PGLOB->smarty->assign_by_ref('fixed_json', json_encode($flex_array));

        $G_PGLOB->smarty->display('send/orderAllocationList.tpl');
    }

}